package com.itheima.grow.model.other;

import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.Map;

/**
 * @author wenzongwei
 * Created on: 2015年10月22日
 */
public class ResponseCode extends Entity {
    private static final long serialVersionUID = -2790656454339569762L;

    public static final String SUCCESS_CODE = "000000";
    public static final String SUCCESS_MESG = "请求处理成功";

    public static final String SYSTEM_FAIL_CODE = "-1";

    public static final String ARGUMENT_INVALID_CODE = "010000";
    public static final String ARGUMENT_INVALID_MESG = "请求参数错误";

    public static final String ENTERPRISE_INVALID_CODE = "010001";

    public static final String MAX_UPLOAD_SIZE_EXCEEDED_CODE = "010002";
    public static final String MAX_UPLOAD_SIZE_EXCEEDED_MESG = "文件大小超过上传限制";

    public static final String RECEIPT_UNMATCH_MESSAGE = "080801";

    //调用adapter响应码
    public static final String INNER_CODE = "200";
    /**
     * 响应码MAP
     */
    private static Map<String, String> RESPONSE_CODE_MAPPING;

    /**
     * 调用E销事故车排班信息接口成功码
     */
    public static final String E_SALES_SUCCESS = "S000A000";

    public static final Boolean SUCCESS_RESULT = true;
    public static final Boolean FAIL_RESULT = false;



    /**
     * 初始化响应码到RESPONSE_CODE_MAPPING中
     */
    public static void setResponseCodeMapping(Map<String, String> responseCodeMapping) {
        RESPONSE_CODE_MAPPING = responseCodeMapping;
    }

    public static String getMessage(final String code) {
        return ResponseMessage.getMessage(code, false);
    }

    public static String getMessage(final String code, final String[] resultMesgParams) {
        return ResponseMessage.getMessage(code, false, resultMesgParams);
    }

    /**
     * 响应消息
     */
    static final class ResponseMessage {
        /**
         * 响应码
         */
        private String code;

        /**
         * 响应内容
         */
        private String mesg;

        /**
         * 消息代码是否无效
         */
        private boolean isInvalidCode = false;

        public ResponseMessage(final String code) {
            this.code = code;
            mesg = null;
        }

        public ResponseMessage(final String code, final String mesg) {
            this.code = code;
            this.mesg = mesg;
        }

        public static String getMessage(final String code) {
            return getMessage(code, false, new String[]{});
        }

        public static String getMessage(final String code, String[] params) {
            return getMessage(code, false, params);
        }

        public static String getMessage(final String code, final boolean showCode) {
            return getMessage(code, showCode, new String[]{});
        }

        public static String getMessage(final String code, final boolean showCode, String[] params) {
            String mesg = RESPONSE_CODE_MAPPING.get(code);
            if (params.length > 0) {
                mesg = MessageFormat.format(mesg, params);
            }
            ResponseMessage responseMessage = new ResponseMessage(code, mesg);

            if (StringUtils.isEmpty(mesg)) {
                responseMessage.isInvalidCode = true;
            }

            return responseMessage.toString(showCode);
        }

        @Override
        public String toString() {
            return this.toString(true);
        }

        public String toString(final boolean showCode) {
            String returnString = "";

            if (showCode) {
                returnString = "[" + code + "] " + mesg;
            } else {
                returnString = mesg;
            }

            return returnString;
        }

        public boolean isInvalidErrorNumber() {
            return isInvalidCode;
        }
    }
}
